package cn.Alibaba.org.service.impl;

import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import cn.Alibaba.basic.util.BaiduAuditUtils;
import cn.Alibaba.org.domain.Employee;
import cn.Alibaba.org.domain.Shop;
import cn.Alibaba.org.domain.ShopLog;
import cn.Alibaba.org.mapper.EmployeeMapper;
import cn.Alibaba.org.mapper.ShopLogMapper;
import cn.Alibaba.org.mapper.ShopMapper;
import cn.Alibaba.org.service.IShopLogService;
import cn.Alibaba.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;


@Service
public class ShopLogServiceImpl extends BaseServiceImpl<ShopLog> implements IShopLogService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopLogMapper shopLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;


    /**
     * 审核通过
     * @param log
     */
    @Transactional
    @Override
    public void pass(ShopLog log) throws MessagingException {

        // 修改状态 state=2
        System.out.println(log.getShop_id());
        Shop shop = shopMapper.loadOne(log.getShop_id());
        System.out.println(shop);
        shop.setState(2);
        shopMapper.update(shop);




        // 添加审核日志到数据库
        log.setState(2);
        log.setAudit_id(325L);
        shopLogMapper.add(log);

        // 发送邮件：审核通过的邮件 叫他激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1912813725@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()+"' ><a href='http://localhost:80/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("aa.jpg",new File("D:\\aa\\aa.jpg"));
        //helper.addAttachment("g1.gif", new File("C:\\Users\\Administrator\\Pictures\\g1.gif"));
        //收件人
        Employee employee = employeeMapper.loadOne(shop.getAdmin_id());
        //helper.setTo(employee.getEmail());
        helper.setTo("1912813725@qq.com");
        javaMailSender.send(mimeMessage);


    }



    /**
     * 审核失败
     * @param log
     */
    @Transactional
    @Override
    public void reject(ShopLog log) throws MessagingException {

        // 修改状态 state=4

        Shop shop = shopMapper.loadOne(log.getShop_id());

        // 添加审核日志到数据库
        log.setState(4);
        log.setAudit_id(325L);
        shopLogMapper.add(log);




        // 发送邮件：审核驳回的邮件 告诉他重新注入
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1912813725@qq.com");
        helper.setSubject("入驻失败邮件");
        helper.setText("<a href='http://localhost:80/#/shopRigester'>点击该链接重新入驻</a>",true);

        //收件人
        Employee employee = employeeMapper.loadOne(shop.getAdmin_id());
        //helper.setTo(employee.getEmail());
        helper.setTo("1912813725@qq.com");
        javaMailSender.send(mimeMessage);


        // 删除管理员信息
        employeeMapper.delete(shop.getAdmin_id());
        shopMapper.delete(shop.getId());

    }
}

